package leetcode.bfs;

import leetcode.bfs.common.BFSUtils;
import leetcode.tree.common.TreeNode;
import leetcode.tree.common.TreeUtils;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution111 {
    public int minDepth(TreeNode root) {
        // 递归求解需要遍历所有节点
//        return TreeUtils.minDepth(root);
        // BFS求解一旦碰到叶子节点就结束, 比递归快很多
        return BFSUtils.minDepth(root);
    }
}

public class leetcode111 {
    public static void main(String[] args) {
        Solution111 solution111 = new Solution111();
        Integer[] nums = {3,9,20,null,null,15,7};
        Integer[] nums1 = {2,null,3,null,4,null,5,null,6};
        test111(solution111, nums);
        test111(solution111, nums1);
    }

    private static void test111(Solution111 solution111, Integer[] nums) {
        TreeNode root = TreeUtils.generateLeetcodeTree(nums);
        System.out.println(solution111.minDepth(root));
    }
}
